// const model = require('../model'); // 数据源  
const userService = require('../service/user'); // 操作数据源
const { APIError, prefix,Pagiation }=require("../until/until");
 

module.exports = { 
	'POST /api/login': async (ctx, next) => {
		 const body=ctx.request.body;
		 let res=await userService.verdictUser({
			 name:body.userName,
			 password:body.password,
		 });  
		 if(res)ctx.send(res) 
	},  
	'POST /api/register': async (ctx, next) => {
		 console.log("register",ctx.request.body);
		 const body=ctx.request.body;
		 let res= await userService.createUser({
		 	name:body.userName,
		 	password:body.password,
		 	gender: '1',
		 	birth:'1990-12-12',
		 }); 
		 ctx.send(res)
	},    
	'GET /api/user': async (ctx, next) => {  
		// console.log("查看ctx对象",ctx,ctx.center); 
		 const body=ctx.request.query; 
		 body.current=Number(body.current);
		 body.size=Number(body.size);
		let param=Object.assign({},new Pagiation(),body); 
		let res=await userService.getUserByParam(param);  
		ctx.send(res);  
	},  
	
	'GET /api/user/:id': async (ctx, next) => { 
		let id=ctx.params.id; 
		let res=await userService.getUserById(id);
		console.log('查询用户: ' + JSON.stringify(res));
		ctx.send(res);  
	},  
	
	'PUT /api/user/:id': async (ctx, next) => { 
		let id=ctx.params.id;
		const body=ctx.request.body; 
		console.log(body);
		let res= await userService.updateUser({
			id,
			name:body.userName, 
			gender:body.sex, 
		});
		ctx.send(res);
	}, 
	'PUT /api/user': async (ctx, next) => {
		let res= await userService.createUser({
			name:"黑猫",
			password:'123456',
			gender: '1',
			birth:'1990-12-12',
		});
		throw new APIError('抛出异常', '具体内容');
	},
	'DELETE /api/user/:id': async (ctx, next) => {
		let id=ctx.params.id;
		// console.log('删除用户',id); 
		let res= await userService.deleteUser(id);
		// console.log('删除用户: ' + JSON.stringify(res)); 
		ctx.send(res);
		// throw new APIError('抛出异常', '具体内容');
	},
	
	
	'DELETE /api/updateUser': async (ctx, next) => {
		// console.log("查看ctx对象",ctx,ctx.center);  
		console.log('创建用户: ' + JSON.stringify(res)); 
	}, 
	'GET /api/template':async (cxt,next)=>{
		cxt.render("index.html",{name:"测试模板引擎"})
	},  
	'GET /api/throw':async (cxt,next)=>{
		throw new APIError('抛出异常', '具体内容');
	},  
	
};
